package whf.framework.tools.generator.cfg;

import org.apache.commons.lang.StringUtils;

import whf.framework.config.ConfigurationFactory;
import whf.framework.config.Configuration;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;

/**
 * @author wanghaifeng
 *
 */
public class ConfigFactory {
	private static Log log = LogFactory.getLog(ConfigFactory.class);
	
	private static ConfigFactory instance = new ConfigFactory();
	
	private Configuration config = null;
	private Configuration config2 = null;

	private ConfigFactory(){
		String pn = this.getClass().getPackage().getName();
		pn = StringUtils.replace(pn,".","/")+"/";
		try{
			config = ConfigurationFactory.getInstance().getConfiguration(pn+"template.config.xml");
			config2 = ConfigurationFactory.getInstance().getConfiguration(pn+"conf.xml");
		}catch(Exception e){
			log.error(this, e);
		}
	}
	
	public static ConfigFactory getInstance(){
		return instance;
	}
	
	public String getTemplateContent(String key){
		String result = this.config.getString(key);
		if(result == null){
			result = this.config.getString(key);
		}
		return result;
	}
	
	private String getString(String module, String key){
		String result = this.config2.getString(module+"."+key);
		if(result == null){
			result = this.config2.getString("default."+key);
		}
		return result;
	}
	
	public boolean getBoolean(String module, String key){
		return "true".equalsIgnoreCase(getString(module, key));
	}
	
	public static Config getConfigInstance(String m){
		Config config = new Config();
		ConfigFactory instance = ConfigFactory.getInstance();
		
		config.setDriver(instance.getString(m,"driver"));
		config.setUrl(instance.getString(m, "url"));
		config.setUsername(instance.getString(m, "username"));
		config.setPassword(instance.getString(m, "password"));
		config.setSchema(instance.getString(m, "schema"));
		config.setTablePrefix(instance.getString(m, "table-prefix"));
		
		config.setTableParser(instance.getString(m, "parserClassName"));
		
		config.setEnableModel(instance.getBoolean(m, "enable-model"));
		config.setEnableDao(instance.getBoolean(m, "enable-dao"));
		config.setEnableDaoImp(instance.getBoolean(m, "enable-daoimp"));
		config.setEnableService(instance.getBoolean(m, "enable-service"));
		config.setEnableServiceImp(instance.getBoolean(m, "enable-serviceimp"));
		config.setEnableAction(instance.getBoolean(m, "enable-action"));
		
		config.setEnableHbm(instance.getBoolean(m, "enable-hbm"));
		config.setEnableConfig(instance.getBoolean(m, "enable-config"));
		config.setEnableMeta(instance.getBoolean(m, "enable-meta"));
		config.setEnableJsp(instance.getBoolean(m, "enable-jsp"));
		config.setEnableWebservice(instance.getBoolean(m, "enable-webservice"));
		
		config.setJavaDir(instance.getString(m, "java-dir"));
		config.setJspDir(instance.getString(m, "jsp-dir"));
		config.setRootPackage(instance.getString(m, "root-package"));
		config.setSubModule(instance.getString(m, "submodule"));
		config.setTables(instance.getString(m, "tables"));
		
		return config;
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(ConfigFactory.getConfigInstance("gjj"));
	}
}
